package com.yanchangapp.ui.presenter;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.PsListRequest;
import com.yanchangapp.model.response.PsListResponse;
import com.yanchangapp.ui.activity.purchase.POrderListActivity;
import com.yanchangapp.ui.view.PsListFgView;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-05.
 * description:
 */
public class PsListFgPresenter extends BasePresenter<PsListFgView> {


    public PsListFgPresenter(BaseActivity context) {
        super(context);
    }

    public void queryPsList(String status,
                            String startTime,
                            String endTime,
                            String keywords,
                            int curPage,
                            boolean isPulling){
        if (getView() != null && curPage == 1 && !isPulling){
            getView().showCoverDialog();
            ((POrderListActivity)mContext).hideCreateBtn();
        }
        PsListRequest psListRequest = new PsListRequest(status,startTime,endTime,keywords,curPage);
        HeadApiRetrofit.getInstance().queryPurchaseList(psListRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PsListResponse>() {
                    @Override
                    public void onNext(PsListResponse psListResponse) {
                        if (getView() != null && curPage == 1 && !isPulling){
                            getView().hideCoverDialog();
                            ((POrderListActivity)mContext).showCreateBtn();
                        }
                        if (psListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (null != getView()){
                                getView().queryPsListSuccess(psListResponse.getData().getList());
                            }
                        }else {
                            if (null != getView()){
                                getView().queryPsListFailure(psListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        // 错误回调,暂时先不处理
                        if (getView() != null){
                            getView().queryPsListFailure(exception.getErrorInfo());
                        }

                        if (getView() != null && curPage == 1 && !isPulling){
                            getView().hideCoverDialog();
                            ((POrderListActivity)mContext).showCreateBtn();
                        }

                    }
                });
    }


    /**
     * 作废订单
     */
    public void dropOrderByID(String orderId){
        if (getView() != null){
            getView().showLoadingDialog();
        }
        HeadApiRetrofit.getInstance().deleteOrderById(orderId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                            if (getView() != null){
                                getView().hideLoadingDialog();
                                getView().dropOrderSuccess();
                            }
                        }else {
                            if (getView() != null){
                                getView().hideLoadingDialog();
                                getView().dropOrderFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().dropOrderFailure(exception.getErrorInfo());
                        }
                    }
                });
    }
}
